import {getHairstylist, getOrderLeaveWord, getShopDetails, getUndoneOrderNumber, reportAudit} from "../../utils/api";
import {getStorage} from "../../utils/util";
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

let baseURL = "http://192.168.0.101:3000";
let page = 1;
let number = 10;
let count: any = 0;
let labelState: any = "";
let renderImageArr: any = [];
let renderServeImageArr: any = [];
let contentMessage = "";
let shopId = "";
let tempShopId = ""
let orderLeaveWordId = ""
Page({
    data: {
        numberPeople: 0,
        windowHeight: 0,
        active: 0,
        renderObj: {},
        hairstylist: [],
        shopMessage: [],
        reportState: false,
        renderImageArr: []
    },
    onLoad(e: any) {
        const {windowHeight} = wx.getWindowInfo();
        this.setData({
            windowHeight
        })
        const id = e.id;
        shopId = id;
        tempShopId = id;
        this.getShopInfo(id);
        this.getHairstylist(id);
        this.onGetShopMessage(id);
    },
    onLeaveWordReport(e: any) {
        orderLeaveWordId = e.currentTarget.dataset.id
        shopId = "";
        this.setData({
            reportState: true
        })
    },
    // 上传举报
    onReport() {
        if (!contentMessage) {
            Toast.fail("还未写入举报原因");
            return
        }
        let obj: any = {};
        if (renderServeImageArr) {
            obj['imageUrl'] = JSON.stringify(renderServeImageArr);
        }
        if (shopId) {
            obj['shopId'] = shopId;
        }
        if (orderLeaveWordId) {
            obj['orderLeaveWordId'] = orderLeaveWordId;
        }
        obj['content'] = contentMessage;
        reportAudit(obj).then(() => {
            Toast.success("举报成功")
        })
    },
    onInputMessage(e: any) {
        contentMessage = e.detail.value
    },
    // 删除图片
    deleteImage(e: any) {
        let arr: any = [];
        let serve: any = [];
        const i = e.detail.index;
        renderImageArr.forEach((val: any, index: any) => {
            if (index == i) {
                // delete renderServeImageArr[i]
            } else {
                arr.push(val);
                serve.push(renderServeImageArr[index])
            }
        })
        renderImageArr = arr;
        renderServeImageArr = serve;
        this.setData({
            renderImageArr: arr
        })
    },
    // 上传图片
    upLoadImage(url: any) {
        wx.uploadFile({
            url: `${baseURL}/api/file/uploadImage`,
            filePath: url,
            name: 'image',
            success: (res: any) => {
                let data = JSON.parse(res.data);
                renderServeImageArr.push({
                    thumb: data.data[0].thumbnail,
                    url: data.data[0].path,
                })
            }
        });
    },
    // 获取本地图片
    getImageURL(e: any) {
        e.detail.file.forEach((res: any, index: any) => {
            renderImageArr.push({url: res.url, isImage: true})
            this.upLoadImage(res.url)
            if (index + 1 == e.detail.file.length) {
                this.setData({
                    renderImageArr: renderImageArr
                })
            }
        })
    },
    // 显示举报界面
    onReportShow() {
        shopId = tempShopId;
        orderLeaveWordId = ''
        this.setData({
            reportState: true
        })
    },
    // 取消举报界面
    onReportClose() {
        this.setData({
            reportState: false
        })
    },
    // 到底加载
    async onGetNextPage() {
        if (page * number >= count) {
            Toast.fail("到底了，给不了您更多了");
            return
        }
        if (labelState) {
        }
    },
    // 获取店铺的评论
    async onGetShopMessage(id: any) {
        const {data} = await getOrderLeaveWord({
            page,
            number,
            shopId: id
        })
        this.setData({
            shopMessage: data.rows
        })
        console.log(data);
    },
    onBreakMap(e: any) {
        const {x, y, name} = e['currentTarget']['dataset'];
        wx.navigateTo({
            url: `../map/map?x=${x}&y=${y}&name=${name}`
        })
    },
    // 获取多少人正在排队
    async onGetUndoneOrderNumber(id: any) {
        const {data} = await getUndoneOrderNumber({
            hairId: id
        })
        return data;
    },
    // 获取当前发型师
    async getHairstylist(id: any) {
        let num = this.data.numberPeople;
        const {data} = await getHairstylist({
            page, number, shopID: id
        })
        count = data.count;
        data.rows.forEach((element: any) => {
            element.practiseTime = Math.floor((+new Date() - element.practiseTime ? (+new Date(element.practiseTime)) : 0) / 31536000000)
            element.uploadTime = Math.floor((+new Date() - (+new Date(element.uploadTime))) / 31536000000)
        });
        data.rows.forEach(async (ele: any) => {
            const data = await this.onGetUndoneOrderNumber(ele['id']);
            ele['lineUpNumber'] = data['lineUpNumber'];
            ele['bookingNumber'] = data['bookingNumber'];
            num = num + data['lineUpNumber'] + data['bookingNumber'];
        });
        this.setData({
            numberPeople: num
        })
        this.setData({
            hairstylist: data.rows
        })
    },
    // 跳转界面
    onBreakHairstylist(e: any) {
        wx.navigateTo({
            url: `../hairstylist/hairstylist?id=${e.currentTarget.dataset.id}`
        })
    },
    async getShopInfo(id: string) {
        // @ts-ignore
        const {userAddress_tb} = await getStorage('userInfo')
        let X = userAddress_tb["longitude"];
        let Y = userAddress_tb["latitude"];
        const {data} = await getShopDetails({id, X, Y});
        data['shopImage'] = JSON.parse(data['shopImage']);
        data['shopAddress'] = JSON.parse(data['shopAddress']);
        this.setData({
            renderObj: data
        })
    },
    tel() {
        wx.makePhoneCall({
            // @ts-ignore
            phoneNumber: (this.data.renderObj['phone'])
        })
    },
    onChange(event: any) {
        console.log(event.detail.index)
    },
    onBackPage() {
        wx.navigateBack({
            delta: 1
        })
    }
})